package com.kefu.call.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Maps;
import com.kefu.call.mapper.CallMakeCallMapper;
import com.kefu.call.mapper.CallStartMapper;
import com.kefu.call.vo.*;
import com.kefu.common.context.KefuContext;
import com.kefu.common.util.DivUtil;
import com.kefu.common.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class CallBigScreenService {
    @Resource
    private CallStartMapper callStartMapper;
    @Resource
    private CallMakeCallMapper callMakeCallMapper;

    /**
     * 呼叫大屏预览数据
     * @param param
     * @return
     */
    public CallBigScreenOverviewVo callOverview(CallBigScreenOverviewSearchVo param) {
        Map<String, Object> queryParam = Maps.newHashMap();
        Map<String, Object> result1 = callMakeCallMapper.callOverview(queryParam);
        Map<String, Object> result2 = callStartMapper.callOverview(queryParam);
        result2.putAll(result1);
        CallBigScreenOverviewVo overviewVo = BeanUtil.toBean(result2,
                CallBigScreenOverviewVo.class, CopyOptions.create().ignoreCase());
        log.info("呼叫概览:{}", JacksonUtil.toString(overviewVo));
        return null;
    }

    /**
     * 回访满意度统计
     * @param param
     * @return
     */
    public CallBigScreenSatisfiedVo callInSatisfied(CallBigScreenOverviewSearchVo param) {
        Map<String, Object> queryParam = Maps.newHashMap();
        CallBigScreenSatisfiedVo satisfiedVo = new CallBigScreenSatisfiedVo();
        return satisfiedVo;
    }

    public List<CallBigScreenOverviewTrendVo> callOverviewTrend(CallBigScreenOverviewSearchVo param) {
        String endTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        String endDay = DateUtil.format(new Date(), "yyyy-MM-dd");
        String startDay = DateUtil.format(new Date(), "yyyy-MM-dd");

        Map<String, Object> queryParam = Maps.newHashMap();
        queryParam.put("startTime", Timestamp.valueOf(startTime));
        queryParam.put("endTime", Timestamp.valueOf(endTime));
        queryParam.put("tenantId", KefuContext.getTid());
        queryParam.put("agent", "");

        queryParam.put("groupType", "date");
        if (startDay.equals(endDay)) {
            queryParam.put("groupType", "hour");
        }

        List<Map<String, Object>> callouts = callMakeCallMapper.callOverviewTrend(queryParam);
        List<Map<String, Object>> callIns = callStartMapper.callOverviewTrend(queryParam);

        Set<String> resultDates = Stream.concat(callouts.stream(), callIns.stream())
                .map(map -> (String) map.get("resultdate"))
                .distinct()
                .collect(Collectors.toSet());
        List<CallBigScreenOverviewTrendVo> list = resultDates.stream()
                .sorted()
                .map(date -> {
                    Map<String, Object> map = Maps.newHashMap();
                    map.put("resultdate", date);
                    return map;
                }).map(map -> {
                    callouts.stream()
                            .filter(data -> data.get("resultdate").equals(map.get("resultdate")))
                            .findAny()
                            .ifPresent(data -> {
                                map.putAll(data);
                            });

                    callIns.stream()
                            .filter(data -> data.get("resultdate").equals(map.get("resultdate")))
                            .findAny()
                            .ifPresent(data -> {
                                map.putAll(data);
                            });

                    return map;
                }).map(map -> {
                    CallBigScreenOverviewTrendVo workVo = BeanUtil.toBean(map, CallBigScreenOverviewTrendVo.class, CopyOptions.create().ignoreCase());
                    return workVo;
                }).map(workVo -> {
                    workVo.setOutCallSuccessRatio(DivUtil.percent(workVo.getOutCallSuccessNumber(), workVo.getOutCallNumber()));
                    workVo.setOutCallTalkTimeAvg(DivUtil.div(workVo.getOutCallTalkTime(), workVo.getOutCallSuccessNumber()));
                    workVo.setAgentAnswerRatio(DivUtil.percent(workVo.getAgentAnswerNumber(), workVo.getCallInTotal()));
                    workVo.setAgentAnswer15Ratio(DivUtil.percent(workVo.getAgentAnswer15Number(), workVo.getAgentAnswerNumber()));
                    workVo.setCallInTalkTimeAvg(DivUtil.div(workVo.getCallInTalkTime(), workVo.getAgentAnswerNumber()));
//                    workVo.setCallInQueuelSuccessRatio(DivUtil.percent(workVo.getCallInQueuelSuccessNumber(), workVo.getCallInQueueNumber()));
//                    workVo.setTime15GiveUpRatio(DivUtil.percent(workVo.getTime15GiveUpNumber(), workVo.getCallInQueuelFailNumber()));
                    return workVo;
                }).map(workVo -> {
                    String resultDate = workVo.getResultDate();
                    if (resultDate.length() != 10) {
                        workVo.setResultDate(resultDate.substring(0, 10));
                        workVo.setResultHour(resultDate.substring(11, 13) + ":00");
                    }
                    return workVo;
                })
                .collect(Collectors.toList());
        return list;
    }
}
